<template>
  <div>
    <el-switch
      v-model="cg"
      active-text="开启数据切换"
      inactive-text="关闭数据切换"
      :active-value="true"
      :inactive-value="false"
      @change="loop"
    >
    </el-switch>

    <div ref="radar" class="radarBox"></div>
  </div>
</template>

<script>

export default {
  data () {
    return {
      // 开关
      cg: false,
      // 配置项
      opt: {
        // title: {
        //   text: '基础雷达图'
        // },
        tooltip: {},
        // legend: {
        //   data: ['张三', '李四']
        // },
        radar: {
          // shape: 'circle',
          axisName: {
            // textStyle: {
            color: '#fff',
            backgroundColor: '#999',
            borderRadius: 3,
            padding: [3, 5]
            // }
          },
          indicator: [
            { name: '工作效率', max: 100 },
            { name: '考勤', max: 100 },
            { name: '积极性', max: 100 },
            { name: '帮助同事', max: 100 },
            { name: '自主学习', max: 100 },
            { name: '正确率', max: 100 }
          ]
        },
        series: [{
          type: 'radar',
          // areaStyle: {normal: {}},
          data: [
            {
              value: [10, 1, 100, 5, 100, 0],
              name: '张三'
            },
            {
              value: [50, 50, 50, 50, 50, 10],
              name: '李四'
            }
          ]
        }]
      }
    }
  },
  mounted () {
    this.drawRadar()
  },
  beforeDestroy () {
    clearInterval(this.st)
  },
  methods: {
    loop () {
      if (this.st) {
        clearInterval(this.st)
        this.st = null
        return
      }
      this.st = setInterval(this.changeData, 1000)
    },
    changeData () {
      const data = []
      for (let index = 0; index < 2; index++) {
        const per = { value: [], name: `李${index}` }
        for (let j = 0; j < 6; j++) {
          per.value.push(Math.floor(Math.random() * 100 + 1))
        }
        data.push(per)
      }
      this.opt.series[0].data = data
      // 2. 重新绘制=》setOption
      this.myChart.setOption(this.opt)
    },

    drawRadar () {
      // 基于准备好的dom，初始化echarts实例
      this.myChart = this.echarts.init(this.$refs.radar)
      // 绘制图表

      this.opt.series[0].data = [
        {
          value: [10, 1, 100, 5, 100, 0],
          name: '张三'
        },
        {
          value: [50, 50, 50, 50, 50, 10],
          name: '李四'
        }
      ]

      this.myChart.setOption(this.opt)
    }
  }
}
</script>

<style lang="scss" scoped>
.radarBox {
  width: 100%;
  height: 400px;
}
</style>
